home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / nos_kit3.zip / UUENCODE.C < prev    next >
C/C++ Source or Header  |  1990-09-16  |  2KB  |  120 lines

  1. /*
  2.  *    Encode a file so it can be mailed to a remote system.
  3.  *
  4.  *    USAGE:
  5.  *        uuencode input [output]
  6.  */
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10.  
  11. /* ENC is the basic 1 character encoding function to make a char printing */
  12. #define ENC(c) (((c) & 077) + ' ')
  13.  
  14. main(argc, argv)
  15. char **argv;
  16. {
  17.     FILE *in, *out;
  18.     struct stat sbuf;
  19.     int mode;
  20.  
  21.     /* non-optional 1st argument */
  22.     if (argc > 1)
  23.     {
  24.         if ((in = fopen(argv[1], "r")) == NULL)
  25.         {
  26.             perror(argv[1]);
  27.             exit(1);
  28.         }
  29.     }
  30.     else
  31.     {
  32.         fprintf(stderr, "Usage: uuencode infile [outfile]\n");
  33.         exit(1);
  34.     }
  35.  
  36.     /* optional 2nd argument */
  37.     if (argc > 2)
  38.     {
  39.         if ((out = fopen(argv[2], "w")) == NULL)
  40.         {
  41.             perror(argv[2]);
  42.             exit(1);
  43.         }
  44.     }
  45.     else
  46.         out = stdout;
  47.  
  48.     /* figure out the input file mode */
  49.     fstat(fileno(in), &sbuf);
  50.     mode = sbuf.st_mode & 0777;
  51.  
  52.     fprintf(out, "begin %o %s\n", mode, argv[1]);
  53.  
  54.     encode(in, out);
  55.  
  56.     fprintf(out, "end\n");
  57.     exit(0);
  58. }
  59.  
  60. /*
  61.  * copy from in to out, encoding as you go along.
  62.  */
  63. encode(in, out)
  64. FILE *in;
  65. FILE *out;
  66. {
  67.     char buf[80];
  68.     int i, n;
  69.  
  70.     for (;;) {
  71.         /* 1 (up to) 45 character line */
  72.         n = fr(in, buf, 45);
  73.         putc(ENC(n), out);
  74.  
  75.         for (i=0; i<n; i += 3)
  76.             outdec(&buf[i], out);
  77.  
  78.         putc('\n', out);
  79.         if (n <= 0)
  80.             break;
  81.     }
  82. }
  83.  
  84. /*
  85.  * output one group of 3 bytes, pointed at by p, on file f.
  86.  */
  87. outdec(p, f)
  88. char *p;
  89. FILE *f;
  90. {
  91.     int c1, c2, c3, c4;
  92.  
  93.     c1 = *p >> 2;
  94.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  95.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  96.     c4 = p[2] & 077;
  97.     putc(ENC(c1), f);
  98.     putc(ENC(c2), f);
  99.     putc(ENC(c3), f);
  100.     putc(ENC(c4), f);
  101. }
  102.  
  103. /* fr: like read but stdio */
  104. int
  105. fr(fd, buf, cnt)
  106. FILE *fd;
  107. char *buf;
  108. int cnt;
  109. {
  110.     int c, i;
  111.  
  112.     for (i=0; i<cnt; i++) {
  113.         c = getc(fd);
  114.         if (c == EOF)
  115.             return(i);
  116.         buf[i] = c;
  117.     }
  118.     return (cnt);
  119. }
  120.